package com.zjitc.servicevideo.controller;


import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.zjitc.common.exception.AuthException;
import com.zjitc.common.utils.RespBean;
import com.zjitc.servicevideo.service.VideoService;
import com.zjitc.servicevideo.utils.InitVideoCilent;
import com.zjitc.servicevideo.utils.VideoUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.ParameterResolutionDelegate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author: ${赵诚挚}
 * @Date: 2024/9/25 17:50
 */
@RestController
@RequestMapping("/servicevideo/video")
@Api(tags = "阿里云视频点播模块")
public class VdController {
    @Autowired
    private VideoService videoService;
    @ApiOperation("上传视频")
    @PostMapping("upload")
    public RespBean uploadVideo(
            @ApiParam(name = "file", value = "文件", required = true)
            @RequestParam("file") MultipartFile file) throws Exception {

        String videoId = videoService.upload(file);
        return RespBean.success().data("videoId", videoId).msg("视频上传成功");
    }


    @ApiOperation("删除视频")
    @DeleteMapping("removeVideo/{videoId}")
    public RespBean removeVideo(@PathVariable String videoId){
        try {
            DefaultAcsClient client = InitVideoCilent.initVodClient(
                    VideoUtils.ACCESS_KEY_ID,
                    VideoUtils.ACCESS_KEY_SECRET);

            DeleteVideoRequest request = new DeleteVideoRequest();

            request.setVideoIds(videoId);

            DeleteVideoResponse response = client.getAcsResponse(request);

            System.out.print("RequestId = " + response.getRequestId() + "\n");
            return RespBean.success().data("data",response).msg("删除视频成功");

        } catch (ClientException e) {
            throw new AuthException("视频删除失败");

        }
    }

}
